$(function () {
    
    $('#username').blur(yuser);
    $('#pwd').blur(ypwd);
    $('#sure').blur(ysure);
    $('#email').blur(yemail);
    $('#nickname').blur(ynick);


    //用户名验证
    function yuser() {
        let username = $('#username').val()
        let reg = /^[a-zA-Z0-9_-]{4,16}$/
        let flag = reg.test(username)
        if (flag) {
            //    $('.user-info').html('格式输入正确').css('color','blue');
            $.ajax({
                type: "post",
                url: "http://139.9.177.51:3333/p2p/accrepeat",
                data: {
                    username: username
                },
                dataTaye: "json",
                async: false,//异步变同步
                success: function (res) {
                    if (res.code == 200) {
                        $('.user-info').html('可以使用这个用户名').css('color', 'blue');
                        $('#username').css('border-color', 'black');
                        flag = true;
                    } else {
                        $('.user-info').html('该用户名重复').css('color', 'red');
                        $('#username').css('border-color', 'red');
                        flag = false;
                    }
                }
            })
        } else {
            $('.user-info').html('注意：不要少于4个字符').css('color', 'red');
            $('#username').css('border-color', 'red');
            flag = false;
        }
        return flag;
    }
    //密码验证
    function ypwd() {
        let reg = /^[a-zA-Z0-9_-]{4,16}$/
        let flag = reg.test($('#pwd').val())
        if (flag) {
            $('.pwd-info').html('格式输入正确').css('color', 'blue');
            $('#pwd').css('border-color', 'black');
            flag = true
        } else {
            $('.pwd-info').html('注意：密码格式不对').css('color', 'red');
            $('#pwd').css('border-color', 'red');
            flag = false
        }
        return flag;
    }
    //再次确定
    function ysure(){
        let pwdval=$('#pwd').val();
        let sureval=$('#sure').val();
        if(pwdval==sureval){
            $('.sure-info').html('密码正确').css('color', 'blue');
            $('#sure').css('border-color', 'black');
        }else{
            $('.sure-info').html('密码错误').css('color', 'red');
            $('#sure').css('border-color', 'red');
        }
    }
    //邮箱验证
    function yemail() {
        let reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
        let flag = reg.test($('#email').val())
        if (flag) {
            $('.email-info').html('格式输入正确').css('color', 'blue');
            $('#email').css('border-color', 'black');
            flag = true;
        } else {
            $('.email-info').html('邮箱格式不正确').css('color', 'red');
            $('#email').css('border-color', 'red');
            flag = false;
        }
        return flag;
    }
    //昵称
    function ynick() {
        let reg = /^[\u4e00-\u9fa5]{0,}$/
        let flag = reg.test($('#nickname').val())
        if (flag) {
            $('.name-info').html('格式输入正确').css('color', 'blue');
            $('#nickname').css('border-color', 'black');
            flag = true;
        } else {
            $('.name-info').html('注意：昵称为汉字').css('color', 'red');
            $('#nickname').css('border-color', 'red');
            flag = flase;
        }
        return flag;
    }
    //注册
    $('#reg-btn').click(function () {
        if(!(yuser()&& ypwd()&&yemail()&&ynick())) return false;

        let username= $('#username').val()
        let pwd= $('#pwd').val()
        let email= $('#email').val()
        let nickname= $('#nickname').val()

        $.ajax({
            type: "post",
            url: "http://139.9.177.51:3333/p2p/reg",
            data: {
                username: username,
                pwd: pwd,
                email: email,
                nickname: nickname
            },
            dataTaye: "json",
            async: false,//异步变同步
            success: function (res) {
              if(res.code==200){
                  alert('注册成功！！！')
                  location.href='./login.html'
              }else{
                alert('注册失败！！！')
              }
            }
        })
    })
})